<?php

namespace app\admin\service;

use app\admin\model\Topic;
use think\facade\Db;

/**
 * 岗位管理-服务类
 * @author 小码哥
 * @since 2023/11/14
 * Class PositionService
 * @package app\admin\service
 */
class TopicService extends BaseService
{
    /**
     * 构造函数
     * @author 小码哥
     * @since 2024/11/14
     *
     * PositionService constructor.
     */
    public function __construct()
    {
        $this->model = new Topic();
    }

    /**
     * @return array
     * @throws \think\db\exception\DbException
     * 获取列表
     */
    public function getList()
    {
        $sort = 'id desc';
        // 常规查询条件
        $param = request()->param();
        $map = [];
        if ($param) {
            // 筛选标题
            if (isset($param['title']) && $param['title']) {
                $map[] = ['title', 'like', "%{$param['title']}%"];
            }
            if (isset($param['type']) && $param['type']) {
                $map[] = ['type', '=', $param['type']];
            }
            // 筛选状态
            if (isset($param['status']) && $param['status']) {
                $map[] = ['status', '=', $param['status']];
            }
        }
        // 设置查询条件
        if (is_array($map)) {
            $map[] = ['mark', '=', 1];
        } elseif ($map) {
            $map .= " AND mark=1 ";
        } else {
            $map .= " mark=1 ";
        }
        $count = $this->model
            ->where($map)
            ->count();
        $ids = $this->model
            ->where($map)
            ->order($sort)
            ->page(PAGE, PERPAGE)
            ->column("id");
        $map[] = ['id', 'in', $ids];
        $field = '';
        $list = $this->model->getAllInfo($map, $field);
        if (empty($list)) {
            //获取数据总数
            //返回结果
            $message = array(
                "msg" => '操作成功',
                "code" => 0,
                "data" => $list,
                "count" => $count,
            );
            unset($list, $count, $map, $param, $ids);
            return $message;
        }
        //获取数据总数
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        unset($list, $count, $map, $param, $ids);
        return $message;
    }

    /**
     * 获取处方下拉列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 小码哥
     * @since 2023/11/14
     */
    public function getTopicSelectList()
    {
        $where = [
            ['status', '=', 1],
            ['mark', '=', 1],
        ];
        $list = $this->model->where($where)
            ->order("id", "desc")
            ->field('id,title')
            ->select()
            ->toArray();
        return message("操作成功", true, $list);
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2023/11/28
     * @author 小码哥
     */
    public function edit()
    {
        //接收参数
        $data = request()->param();
        $res = parent::edit($data);
        if (isset($data['id']) && !empty($data['id'])) {
            $this->upPrescriptionScore($data['id'], $data['all_score']);
        }// TODO: Change the autogenerated stub
        return $res;
    }

    /**
     * 更新题库下的处方数量
     */
    public function upPrescriptionScore($topic_id, $all_score)
    {
        Db::name('prescription')
            ->where('topic_id', $topic_id)
            ->update(['all_score' => $all_score]);
        return;
    }

    /**
     * 获取处方下拉列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 小码哥
     * @since 2023/11/14
     * 先判断有题目的处方 然后在有题目的处方中找到这些题库
     */
    public function getExamTopicSelectList1()
    {
        $prescriptionIds = Db::name('prescription')
            ->where('type', 1)
            ->where('mark', 1)
            ->where('num', '>', 0)
            ->column('id');
        $where = [
            ['status', '=', 1],
            ['mark', '=', 1],
            ['type', '=', 1],
        ];
        $list = $this->model
            ->where($where)
            ->whereIn('id', $prescriptionIds)
            ->order("sort", "asc")
            ->field('id,title,all_score')
            ->select()
            ->toArray();
        return message("操作成功", true, $list);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取题库列表
     */
    public function getExamTopicSelectList()
    {
        $where = [
            ['status', '=', 1],
            ['mark', '=', 1],
            ['type', '=', 1],
        ];
        $list = $this->model
            ->where($where)
            ->order("sort", "asc")
            ->field('id,title')
            ->select()
            ->toArray();
        return message("操作成功", true, $list);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getExamTopicSelect()
    {
        $where = [
            ['status', '=', 1],
            ['mark', '=', 1],
            ['type', '=', 1],
        ];
        $topicIds = Db::name('prescription')
            ->where($where)
            ->where('num', '>', 0)
            ->column('topic_id');
        $list = $this->model
            ->where($where)
            ->whereIn('id', $topicIds)
            ->order("id", "desc")
            ->field('id,title,all_score')
            ->select()
            ->toArray();
        return message("操作成功", true, $list);
    }

    /**
     * 获取处方下拉列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 小码哥
     * @since 2023/11/14
     */
    public function getTrainTopicSelectList()
    {
        $where = [
            ['status', '=', 1],
            ['mark', '=', 1],
            ['type', '=', 2],
        ];
        $list = $this->model
            ->where($where)
            ->order("sort", "asc")
            ->field('id,title')
            ->select()
            ->toArray();
        return message("操作成功", true, $list);
    }


}